package chat7me_1223.client.ui;

import chat7me_1223.client.Client;
import chat7me_1223.client.util.IOUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class LoginWindow {
    JFrame win=null;
    JPanel contentPane=null;
    JLabel accountLabel=null;
    JTextField accountInput=null;
    JLabel pwdLabel=null;
    JPasswordField pwdInput=null;
    JButton loginBtn=null;
    JButton registerBtn=null;
    InputStream in;
    OutputStream out;
    Client client;
    public LoginWindow(Client c){
        this.client=c;
        if(client!=null){
            client.createSocket();
        }
        win=new JFrame("登录窗口");
        contentPane=new JPanel();
        contentPane.setLayout(null);

        accountLabel=new JLabel("账号：");
        accountLabel.setBounds(new Rectangle(18,53,335,38));
        accountLabel.setFont(new Font("Dialog",Font.BOLD,14));

        accountInput=new JTextField( );
        accountInput.setBounds(new Rectangle(84,56,260,30));//宽高，处于界面的位置

        pwdLabel=new JLabel("密码：");
        pwdLabel.setBounds(new Rectangle(18,107,335,38));

        pwdInput=new JPasswordField();
        pwdInput.setBounds(new Rectangle(84,111,260,30));//宽高，处于界面的位置;

        loginBtn=new JButton("登录");
        loginBtn.setBounds(new Rectangle(143,162,90,31));
        loginBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doLogin();
            }
        });

        registerBtn=new JButton("注册");
        registerBtn.setBounds(new Rectangle(295,214,68,20));
        registerBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeLoginWin();
                client.setRegWindow(new RegWindow(client));
            }
        });

        contentPane.add(accountLabel);
        contentPane.add(accountInput);
        contentPane.add(pwdLabel);
        contentPane.add(pwdInput);
        contentPane.add(loginBtn);
        contentPane.add(registerBtn);
        win.add(contentPane);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setSize(390,280);
        win.setLocation(400,200);
        win.setVisible(true);

    }

    public JButton getLoginBtn() {
        return loginBtn;
    }

    public JButton getRegisterBtn() {
        return registerBtn;
    }

    public void closeLoginWin() {
        win.dispose();
        client.setLoginWindow(null);
    }

    private void doLogin() {
        try {
            if(this.client.getSocket()!=null){
                if(accountInput.getText()!=null && !"".equals(accountInput.getText())&&
                        pwdInput.getText()!=null && !"".equals(pwdInput.getText())){
                    if(in==null){
                        in=this.client.getSocket().getInputStream();
                    }
                    if(out==null){
                        out=this.client.getSocket().getOutputStream();
                    }
                    loginBtn.setEnabled(false);
                    this.client.setLoginUsername(accountInput.getText());
                    IOUtil.writeShort(Client.LOGIN_REQUEST,out);
                    IOUtil.writeString(accountInput.getText(),out);
                    IOUtil.writeString(pwdInput.getText(),out);
                }else{
                    JOptionPane.showMessageDialog(null, "用户名或密码为空");
                }
            }
        }catch (IOException e){
            JOptionPane.showMessageDialog(null, "网络异常，原因："+e.getMessage());
        }
    }


}
